import apis from '@/api/index'

export default function ({ $axios, store, redirect }, inject) {
    function axiosConfig($axios) {
        let requestConfig = {}
        // 设置API的域名
        $axios.setBaseURL(process.env.BASE_URL)
        $axios.defaults.timeout = 5000

        // 设置请求拦截
        $axios.onRequest((config) => {
            requestConfig = {
                baseURL: config.baseURL,
                url: config.url,
                method: config.method,
                data: config.data,
                headers: config.headers,
                params: config.params,
            }

            config.startTime = new Date().getTime()
            config.headers['Content-Type'] = 'application/json'
            /* 如果你需要token */
            const token = store.state.token || ''
            if (token) {
              config.headers.Authorization = `Bearer ${token}`
            }
            return config
        })
        // 设置响应拦截
        $axios.onResponse((response) => {
            response.config.endTime = new Date().getTime()
            const status = response.status

            if (status === 200) {
                // 打印出每个接口的响应时间，如果慢了就捶后端，让他优化！！！
                console.info(response.config.url, '请求时间', response.config.endTime - response.config.startTime + 'ms'
                )

                // 返回接口数据
                return response.data
            } else {
                // 如果请求失败的，打印出相应的错误信息，更好的修改。
                const responseConfig = response ? response.config : {}
                console.error('响应拦截报错提示： ', {
                    url: responseConfig.baseURL + responseConfig.url,
                    status: response.status,
                    statusText: response.statusText,
                    method: responseConfig.method,
                    headers: responseConfig.headers,
                    data: responseConfig.data,
                    params: responseConfig.params,
                    responseData: response.data,
                })
            }
        })

        // axios错误处理
        $axios.onError((error) => {
            redirect('/error')
            const response = error.response || {}
            const responseConfig = response.config || {}
            console.error('$axios.onError: ', error)
            console.error('错误处理提示 ', {
                url: responseConfig.baseURL + responseConfig.url,
                status: response.status,
                statusText: response.statusText,
                method: responseConfig.method,
                headers: responseConfig.headers,
                data: responseConfig.data,
                params: responseConfig.params,
                responseData: response.data,
                ...requestConfig,
            })

        })
        // 最后返回$axios对象
        return $axios
    }

    // 使用plugin中inject参数暴露api ,api注入到服务端context, vue实例, vuex中。
    // 文档: https://www.nuxtjs.cn/guide/plugins
    // inject:注入到服务端context, vue实例, vuex中
    const apiObject = {};
    for (const i in apis) {
        apiObject[i] = apis[i](axiosConfig($axios.create()));
    }

    inject("api", apiObject);
}

